class Index {
    constructor() {
        footer()
        
        this.classifyindex()
        this.initSwiper()
        this.Home()
        this.index = 0
        this.principal = []
        this.onbeforeunload()
    }

    onbeforeunload() {
       
        window.onbeforeunload =  ()=> {
           let ee= getItem("principalIII")
          setItem("principal",ee)
        }
    }
    //首页分类
    async classifyindex() {
        console.log("首页")

        if (!getItem("principal")) {
            let principalh = await indexserve.type()

            let { additional, principal } = principalh.result
            this.principal = principal;
            //已选
            setItem("principal", principal)
            //刷新
            setItem("principalIII", principal)
            //未选
            setItem("additional", additional)
            //收藏
            setItem("like",[])
            //历史记录
            setItem("History",[])
        } else {
            this.principal = getItem("principal")
            console.log()
        }
        this.principal.forEach((v, i) => {
            //添加分类Li
            $(".header_menu ul").html(`
                 <li class="${i == 0 ? "active" : ""}">${v.title} </li>
            `)
            $(".swiper-wrapper").html(`
               <div class="swiper-slide">
                     <ul class="qwe${i}"></ul>
               </div>
        `)
        });

        _(".header_menu ul li").tap(({ v, i }) => {
            this.swiper.slideTo(i)

        })

        _(".swiper-wrapper .swiper-slide").forEach(v => {
            v.onscroll = () => {
                const { clientHeight, scrollHeight, scrollTop } = v;

                let distance = scrollHeight - scrollTop - clientHeight;
                if (distance <= 1) {
                    if (this.index != 0) {
                        this.principal[this.index].page += 1;
                        setItem("principal", this.principal)
                        if (this.principal[this.index].page <= this.principal[this.index].total_page) {
                            this.classifications('ko')

                        }
                    }
                } else {

                }
            }
        })

    }

    initSwiper() {
        let self = this
        this.swiper = new Swiper('.swiper', {
            direction: 'horizontal',
            on: {
                slideChangeTransitionStart: function () {
                    self.index = this.activeIndex
                    _(".header_menu ul li")[this.activeIndex].appClass("active")
                        .siblings().removeClass("active");
                    console.log("中")
                    self.classifications()

                },
            },
        })
    }
    //首页
     ddf
    async Home() {
        let xq = await indexserve.index();
        console.log( xq.result.list)
        this.ddf=()=>{
            item(_(".swiper-wrapper .swiper-slide ul")[0], xq.result.list)
        }
       this.ddf();
        clicka(".swiper-wrapper .swiper-slide ul li", xq.result.list, "detail.html")
    }

    async classifications(type = "click") {
        console.log("未")
        console.log(this.principal)
        this.principal = getItem("principal")
        let loadData = async () => {
            let red = await indexserve.mechanismList({
                id: this.principal[this.index].id,
                page: this.principal[this.index].page
            })
            console.log(red)
            //将值赋给分类中的变量
            // console.log(red)
            red.result.data.forEach(v=>{
                this.principal[this.index].content.push(v);
            })
            this.principal[this.index].total_page = red.result.total_page;
            console.log(this.principal[this.index].content)
            setItem("principal", this.principal)
            // console.log(this.principal.result.principal[this.index].content)
            item(
                _(".swiper-wrapper .swiper-slide ul")[this.index],
                this.principal[this.index].content
             )
            
            console.log(this.principal[this.index].content)
            let fff = `.swiper-wrapper .swiper-slide .qwe${this.index} li`
           clicka(fff, this.principal[this.index].content, "detail.html")
            this.ddf();
        }

        if (type == "click") {
            if (this.principal[this.index].content.length == 0) {
                this.principal[this.index].page += 1
                loadData()
                
            }
        } else {
            loadData()
            
        }
        
    }
}

new Index()